//
//  AccountSettingVC.m
//  ImGuider
//
//  Created by llt on 2017/5/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AccountSettingVC.h"
#import "FileManager.h"
#import "AccountCell.h"

@interface AccountSettingVC ()

@property (weak, nonatomic) IBOutlet UISwitch *playSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *downloadSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *receiverSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *allPlaySwitch;

@property (weak, nonatomic) IBOutlet UILabel *cacheSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *currentLanguageLabel;
@property (weak, nonatomic) IBOutlet UILabel *currentCurrencyLabel;

@property (weak, nonatomic) IBOutlet UILabel *logoutLabel;

@property (weak, nonatomic) IBOutlet UILabel *playLabel;
@property (weak, nonatomic) IBOutlet UILabel *downloadLabel;
@property (weak, nonatomic) IBOutlet UILabel *receiverLabel;
@property (weak, nonatomic) IBOutlet UILabel *allPlayLabel;

@property (weak, nonatomic) IBOutlet UILabel *clearLabel;

@property (weak, nonatomic) IBOutlet UILabel *languageLabel;
@property (weak, nonatomic) IBOutlet UILabel *currencyLabel;

@property (weak, nonatomic) IBOutlet UILabel *aboutusLabel;

@property (weak, nonatomic) IBOutlet UILabel *serverLabel;
@property (nonatomic, strong) NSDictionary *serverDict;

@end

@implementation AccountSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self reloadTB];
}

- (void)setup {
    
    self.title = MYLocalizedString(@"ACCOUNT_SETTING", nil);
    
    self.playLabel.text = MYLocalizedString(@"SETTING_LISTEN_ASK", nil);
    self.downloadLabel.text = MYLocalizedString(@"SETTING_DOWNLOAD_ASK", nil);
    self.receiverLabel.text = MYLocalizedString(@"SETTING_RECEIVER_ASK", nil);
    self.languageLabel.text =MYLocalizedString(@"SETTING_LANGUAGE_SET", nil);
    self.clearLabel.text = MYLocalizedString(@"SETTING_CLEAR_CACHE", nil);
    self.aboutusLabel.text = MYLocalizedString(@"SETTING_ABOUTUS", nil);
    self.allPlayLabel.text = MYLocalizedString(@"SETTING_LOOP_MODE", nil);
    self.logoutLabel.text = MYLocalizedString(@"SETTING_LOGOUT", nil);
    
    self.currencyLabel.text = MYLocalizedString(@"SETTING_CURRENCY_TITLE", nil);
    self.tableView.backgroundColor = kBGColor;
    
    self.serverDict = @{@"https://www.imguider.com/tourist/services/":@"正式服务器",
                        @"http://hz.imguider.com/tourist/services/":@"杭州服务器",
                        @"http://domestic.imguider.com/tourist/services/":@"测试服务器"};
    
    
    [self reloadTB];
    
}

- (void)reloadTB {
    
    NSInteger size = [FileManager getResponseCacheSize] + [UIImageView imageCacheSize];
    NSString *fileSize = [IGUtil changeFileSize: size];
    self.cacheSizeLabel.text = fileSize;
    
    self.playSwitch.on = [AppManager sharedManager].allowWhenPlay;
    self.downloadSwitch.on = [AppManager sharedManager].allowWhenDownload;
    self.receiverSwitch.on = ![AppManager sharedManager].isOffReceiverWhenListen;
    self.allPlaySwitch.on = [AppManager sharedManager].allPlayWhenListen;
    
    self.serverLabel.text = self.serverDict[[AppManager sharedManager].baseURLStr];
    
    NSDictionary *dict = @{@(AppLanguageCNJan) : @"简体中文", @(AppLanguageCNFan) : @"繁體中文（台灣）", @(AppLanguageCNFanXG) : @"繁體中文（香港）", @(AppLanguageEnglish) : @"English", @(AppLanguageJapanese) : @"日本語",@(AppLanguageItaliano) : @"Italiano"};
    
    
    NSString * language = dict[@([AppManager sharedManager].language)];
    
    self.currentLanguageLabel.text = language;
    
    if ([[AppManager sharedManager].currencyCode isEqualToString:@"CNY"]) {
        self.currentCurrencyLabel.text = MYLocalizedString(@"SETTING_CURRENCY_CNY", nil);
    } else {
        self.currentCurrencyLabel.text = MYLocalizedString(@"SETTING_CURRENCY_USD", nil);
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 4;
    }
    
    if (section == 2) {
        
        if ([UserManager sharedManager].user.role == UserRolesTypeAdmin) {
            return 4;
        }
        return 3;
    }
    
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if ([UserManager sharedManager].token) {
        return 4;
    }
    return 3;
}

- (IBAction)changeState:(UISwitch *)sender {
    
    if (sender == self.playSwitch) {
        [AppManager sharedManager].allowWhenPlay = sender.on;
    } else if (sender == self.downloadSwitch) {
        [AppManager sharedManager].allowWhenDownload = sender.on;
    } else if (sender == self.receiverSwitch) {
        [AppManager sharedManager].isOffReceiverWhenListen = !sender.on;
    } else if (sender == self.allPlaySwitch) {
        [AppManager sharedManager].allPlayWhenListen = sender.on;
    }
}


#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section) {
        case 0: {
            
        }
            break;
        case 1: {
            
            if (indexPath.row == 0) {// 清理缓存
            
                [IGAlertView alertWithTitle:@"" message:MYLocalizedString(@"SETTING_CLEAR_ASK", nil) cancelButtonTitle:MYLocalizedString(@"PUBLIC_CANCEL", nil) commitBtn:MYLocalizedString(@"PUBLIC_CONFIRM", nil) commit:^{
                    [FileManager deleteResponseCache];
                    
                    [UIImageView clearImageCache];
                    [self reloadTB];
                } cancel:nil];
            }
        }
            break;
        case 2: {
            
            
            if (indexPath.row == 3) {
                
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                
                for (NSString *server in self.serverDict.allKeys) {
                    
                    @weakify(self);
                    UIAlertAction *action = [UIAlertAction actionWithTitle:self.serverDict[server] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        @strongify(self);
                        [AppManager sharedManager].baseURLStr = server;
                        [IGUtil showToast:@"服务器更改成功,请重启应用"];
                        [self reloadTB];
                    }];
                    
                    [action setValue:kThemeColor forKey:@"titleTextColor"];
                    
                    [alertController addAction:action];
                }
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:MYLocalizedString(@"PUBLIC_CANCEL", nil) style:UIAlertActionStyleCancel handler:nil];
                
                [cancelAction setValue:kThemeColor forKey:@"titleTextColor"];
                [alertController addAction:cancelAction];
                
                [self presentViewController:alertController animated:YES completion:nil];
            }
            
        }
            break;
        case 3: {
            
             if (indexPath.row == 0) { // 退出登录
                 
                 @weakify(self);
                 [IGAlertView alertWithTitle:@"" message:MYLocalizedString(@"SETTING_LOGOUT_ALERT_COUTENT", nil) cancelButtonTitle:MYLocalizedString(@"PUBLIC_CANCEL", nil) commitBtn:MYLocalizedString(@"PUBLIC_CONFIRM", nil) commit:^{
                     @strongify(self);
                     [self logout];
                 } cancel:nil];
             }
        }
            break;
            
        default:
            break;
    }
}

- (void)logout {
    
    [ActivityIndicatorView showHUDAddedTo:self.tableView];
    
    @weakify(self);
    [NetAPIManager logoutWithBlock:^(NSString *result, NSError *error) {
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.tableView];
        if (!error) {
            [IGUtil showToast:MYLocalizedString(@"SETTING_LOGOUT_SUCCESS", nil)];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

@end
